package com.pzhu.servlet;

import com.pzhu.controller.UserController;
import com.pzhu.controller.impl.UserControllerImpl;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    //注入
    private UserController userController = new UserControllerImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        long account = Long.parseLong(req.getParameter("account"));
        System.out.println(account);
        String password = req.getParameter("password");
        System.out.println(password);
        boolean flag = userController.checkUser(account, password);
        if (flag == true) {
            System.out.println("登录成功");
            Cookie cookie = new Cookie("Uid",req.getParameter("account"));
            cookie.setMaxAge(3 * 24 * 3600);
            cookie.setPath("/library/main");
            resp.addCookie(cookie);
            HttpSession session = req.getSession();
            session.setMaxInactiveInterval(1 * 1 * 3600);//设置生存时间 单位 /秒
            session.setAttribute("login",true);
            session.setAttribute("Uid", userController.selectUser(account).getAccount());
            session.setAttribute("Uname", userController.selectUser(account).getName());
            resp.sendRedirect("show-all-books");
        } else {
            System.out.println("登陆失败");
            req.getRequestDispatcher("/view/jsp/login.jsp").forward(req, resp);
        }
    }
}
